
import 'package:flutter_fun_zjversion/config/net/wan_android_api.dart';
import 'package:flutter_fun_zjversion/model/User.dart';
import 'package:flutter_fun_zjversion/model/article.dart';
import 'package:flutter_fun_zjversion/model/banner.dart';

class WanAndroidRepository {
  static Future fetchBanners() async {
    var response = await http.get("banner/json");
    return response.data.map<Banner>((item) => Banner.fromJsonMap(item)).toList();
  }

  static Future fetchTopArticles() async{
    var response = await http.get("article/top/json");
    return response.data.map<Article>((item)=>Article.fromMap(item)).toList();
  }


  static Future fetchArticles(int pageNum, {int cid}) async{
    var response = await http.get('article/list/$pageNum/json',
        queryParameters: (cid != null ? {'cid': cid} : null));
    return response.data['datas'].map<Article>((item)=>Article.fromMap(item)).toList();
  }


  static Future login(String name,String pass) async{
    var response = await http.post<Map>('user/login',queryParameters: {
      'username': name,
      'password': pass,
    });
    return User.fromJsonMap(response.data);
  }
  static Future loginOut() async{
    /// 自动移除cookie
    await http.get('user/logout/json');
  }

  // 收藏列表
  static Future fetchCollectList(int pageNum) async {
    var response = await http.get<Map>('lg/collect/list/$pageNum/json');
    return response.data['datas']
        .map<Article>((item) => Article.fromMap(item))
        .toList();
  }



  // 收藏
  static collect(id) async {
    await http.post('lg/collect/$id/json');
  }

  // 取消收藏
  static unCollect(id) async {
    await http.post('lg/uncollect_originId/$id/json');
  }

  // 取消收藏2
  static unMyCollect({id, originId}) async {
    await http.post('lg/uncollect/$id/json',
        queryParameters: {'originId': originId ?? -1});
  }
}